package com.itheima.shop.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.itheima.shop.entity.Product;
import com.itheima.shop.service.ProductService;

@Controller
@RequestMapping("/product")
public class ProductController {
	
	private final static Logger logger = LoggerFactory.getLogger(ProductController.class);
	@Autowired
	private ProductService service;
	
	@Autowired
	private HttpServletRequest request;
	
	@RequestMapping("/product_list")
	public String productList(Integer cid,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "12")Integer pageSize,String oderBy) {
		PageInfo<Product> productPage = service.findByCid(cid, pageNum, pageSize, oderBy);
		logger.info("productList:"+productPage.toString()+",pages:"+productPage.getPages());
		request.setAttribute("productPage", productPage);
		return "forward:/product_list.jsp";
	}
	
	@RequestMapping("/product_info")
	public String productInfo(Integer pid) {
		Product product = this.service.findByPid(pid);
		request.setAttribute("product", product);
		return "forward:/product_info.jsp";
	}
}
